package org.sith.taskrunner.ejb.services.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

import org.sith.taskrunner.api.user.Role;
import org.sith.taskrunner.api.user.User;
import org.sith.taskrunner.ejb.annotations.AllowedRoles;
import org.sith.taskrunner.ejb.db.dao.UserDAO;
import org.sith.taskrunner.ejb.db.entity.UserEntity;
import org.sith.taskrunner.ejb.interceptors.PermissionsChecker;
import org.sith.taskrunner.ejb.services.UserManagerService;
import org.sith.taskrunner.util.MD5;

@Interceptors(PermissionsChecker.class)
@Stateless(name = "UserManagerEJB")
public class UserManager implements UserManagerService {

	@EJB(name = "UserDAOService")
	UserDAO userDAOService;

	@Override
	public User getUserByNameAndPassword(String usrName, String usrPass) {
		User user = userDAOService.getUserByNameAndPassword(usrName, usrPass);
		return user;
	}

	@Override
	@AllowedRoles(Role.ADMIN)
	public List<String> getAllUserWithUserRole() {
		List<String> userList = getUserWithRole(Role.USER);
		return userList;
	}

	@Override
	@AllowedRoles(Role.ADMIN)
	public List<String> getAllUserWithAdminRole() {
		List<String> userList = getUserWithRole(Role.ADMIN);
		return userList;
	}

	@Override
	@AllowedRoles(Role.ADMIN)
	public List<String> getAllUserWithTaskOwnerRole() {
		List<String> userList = getUserWithRole(Role.TASK_OWNER);
		return userList;
	}

	@Override
	@AllowedRoles(Role.ADMIN)
	public List<UserEntity> getAllUsers() {
		List<UserEntity> users = userDAOService.findAll();
		return users;
	}

	@Override
	@AllowedRoles(Role.ADMIN)
	public Map<String, Role> getUsersAndRoles() {
		List<UserEntity> users = userDAOService.findAll();
		Map<String, Role> usersMap = new HashMap<String, Role>();
		for (int i = 0; i < users.size(); i++) {
			if (users.get(i) != null) {
				usersMap.put(users.get(i).getLogin(), users.get(i).getRole());
			}
		}
		return usersMap;
	}

	@Override
	@AllowedRoles(Role.ADMIN)
	public int getUsersCount() {
		List<UserEntity> users = userDAOService.findAll();
		return users.size();
	}
	@AllowedRoles(Role.ADMIN)
	@Override
	public void addUser(UserEntity user){
		String password = user.getPassword();
		user.setPassword(MD5.getHashCode(password));
		userDAOService.saveOrUpdate(user);
	}

	private List<String> getUserWithRole(Role role) {
		List<UserEntity> userList = userDAOService.getUserByRole(role);
		if (userList == null) {
			return null;
		}
		List<String> currentList = new ArrayList<String>(userList.size());
        for (UserEntity anUserList : userList) {
            if (anUserList != null) {
                currentList.add(anUserList.getLogin());
            }
        }
		return currentList;
	}

    @AllowedRoles(Role.ADMIN)
	@Override
	public void removeUserById(Long id) {
		userDAOService.removeById(id);
	}


    @AllowedRoles(Role.ADMIN)
    @Override
    public boolean updateActiveState(Long id, boolean active) {
        return userDAOService.updateUserActiveState(id,active);
    }


    @AllowedRoles(Role.ADMIN)
    @Override
    public boolean updateRole(Long id, Role roleEntity) {
        return  userDAOService.updateRole(id,roleEntity);
    }


}
